package com.cskaoyan.user.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;

import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.*;
import com.cskaoyan.user.form.PostLogin;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 创建日期: 2022/05/19 11:13
 *
 * @author JinLu Zhou
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    IKaptchaService iKaptchaService;
    @Autowired
    ILoginService iLoginService;


    @PostMapping("/login")
    public ResponseData login(@RequestBody PostLogin postLogin, HttpServletRequest request, HttpServletResponse servletResponse){
        String captcha = postLogin.getCaptcha();
        String userName = postLogin.getUserName();
        String userPwd = postLogin.getUserPwd();
        //验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String kaptcha_uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (!kaptchaCodeResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setPassword(userPwd);
        userLoginRequest.setUserName(userName);
        UserLoginResponse response = iLoginService.login(userLoginRequest,servletResponse);
        if (response.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            //登录成功
            ResponseData<Object> responseData = new ResponseData<>();
            responseData.setResult(response);
            responseData.setSuccess(true);
            responseData.setMessage("success");
            responseData.setCode(200);
            responseData.setTimestamp(System.currentTimeMillis());
            return responseData;
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("/login")
    public ResponseData login(HttpServletRequest request){
        UserLoginResponse response = iLoginService.login(request);
        if (response.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            //已经登录
            ResponseData<Object> responseData = new ResponseData<>();
            UserLoginResponseGet userLoginResponseGet = new UserLoginResponseGet();
            userLoginResponseGet.setUid(response.getId());
            userLoginResponseGet.setUsername(response.getUsername());
            responseData.setResult(userLoginResponseGet);
            responseData.setSuccess(true);
            responseData.setMessage("success");
            responseData.setCode(200);
            responseData.setTimestamp(System.currentTimeMillis());
            return responseData;
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
